<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>冒泡算法</title>
</head>
<body>
	<script type="text/javascript">
		function getArr(size) {
			var arr = new Array();
			for (let i = size; i > 0; i--) {
				arr.push(i)
			}
			return arr;
		}
		// 冒泡
		function bubble1(arr) {
			var time = (new Date()).getTime();
			var newarr = arr,
				len = newarr.length;
			var _swap = function (index1, index2) {
				var aux = newarr[index1];
				newarr[index1] = newarr[index2];
				newarr[index2] = aux;
			}

			for (let i = 0; i < len; i++) {
				for (let j = 0; j < len - 1 ; j++) {
					if (newarr[j] > newarr[j + 1]) {
						_swap(j , j+1);
					}
					console.log(1)
				}
			} 
				console.log((new Date()).getTime() - time)
				
			return newarr;
		}
		function bubble2(arr) {
			var time = (new Date()).getTime();
			var newarr = arr,
				len = newarr.length;
			var _swap = function (index1, index2) {
				var aux = newarr[index1];
				newarr[index1] = newarr[index2];
				newarr[index2] = aux;
			}

			for (let i = 0; i < len; i++) {
				for (let j = 0; j < len - 1 - i; j++) {
					if (newarr[j] > newarr[j + 1]) {
						_swap(j , j+1);
					}
					console.log(1)
				}
			} 
				console.log((new Date()).getTime() - time)
				
			return newarr;
		}
		console.log(bubble1(getArr(10)));
		console.log(bubble2(getArr(10)));
		
	</script>
</body>
</html>